package com.sca.webservice.service;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @Author: denglei
 * @Date: 2022/1/13 09:38
 * @Description:
 */
public class TestDemo {
    public static final String address="http://127.0.01:9088/services/user?wsdl";


    /**
     * 使用代理类工厂
     */
    public static void test1() {
        try {
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(TestService.class);
            // 创建一个代理接口实现
            TestService cs = (TestService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String LineId = "1";
            // 调用代理接口的方法调用并返回结果
           /* User result = (User)cs.getUser(LineId);
            System.out.println("返回结果:" + result);*/
            //返回值为：返回结果:User{id=1, name='lisi'}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 动态调用方式
     */
    public static void test2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(address);

        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getUserName", "1");
            System.out.println("返回数据:" + objects[0]);
            //返回值为：返回数据:{"result":{},"success":true,"message":"zhangsan"}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        test2();
    }
}
